import Api from "@/api/api-declare";
import type { ApiSaasTypes } from "@/api/api-types/api-saas-types";
import Store from "@/store";

export function useSaasChannel() {

	const channelInfo = ref<ApiSaasTypes.GetSaasChannelRes>({
		account_id: 0,//钱包id
		channel_id: 0,//渠道id
		fleet_introduction: '',//saas渠道简介
		fleet_logo: '',//saas渠道logo图
		fleet_name: '',//saas车队名称
		mobile: '',//saas客服电话
	});

	const getChannelInfo = (channel_id: number, save2store = true) : Promise<typeof channelInfo.value> => {
		return new Promise((r) => {
			Api.ApiSaas.getSaaSchannelInfo({ channel_id }).then(res => {
				channelInfo.value = res;
				if (save2store) {
					Store.inviter().saasChannelInfo = res;
				};
				r(channelInfo.value);
			});
		});
	};
	
	const joinSaasListQuery = ref({page : 1});
	const joinSaasList = ref<ApiSaasTypes.ChannelListItem[]>([]);
	const joinSaasTotal = ref(0);
	//获取加入saas渠道的列表
	const getJoinSaasList = () : Promise<void> => {
		return new Promise((r) => {
			Api.ApiSaas.getChannelList(joinSaasListQuery.value).then(res => {
				joinSaasList.value = [...joinSaasList.value,...res.list];
				joinSaasTotal.value = res.total;
				r();
			});
		});
	};
	//退出saas渠道
	const exitSaasChannel = (channel_id : number) : Promise<void> => {
		return new Promise((r) => {
			Api.ApiSaas.exitSaasChannel({channel_id:channel_id}).then(() => {
				joinSaasList.value = joinSaasList.value.filter(i => i.channel_id !== channel_id );
				r();
			});
		});
	};
	return {
		channelInfo,
		getChannelInfo,
		joinSaasListQuery,
		getJoinSaasList,
		joinSaasList,
		joinSaasTotal,
		exitSaasChannel
	};
};